package cn.stylefeng.guns.modular.leaveinfo.controller;


import java.util.List;
import java.util.Map;


import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.leaveinfo.model.LeaveInfoParam;
import cn.stylefeng.guns.modular.leaveinfo.model.LeaveInfoResult;
import cn.stylefeng.guns.modular.leaveinfo.service.LeaveInfoService;
import cn.stylefeng.guns.sys.modular.system.entity.LeaveInfoPO;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/leaveInfo")
public class LeaveInfoController {

    @Autowired
    private LeaveInfoService leaveInfoService;

    private String PREFIX = "/leaveInfo";

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/leaveInfo.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/leaveInfo_add.html";
    }

    /**
     * 详情页面
     */
    @RequestMapping("/leaveInfo_detail")
    public String leaveInfoDetail() {
        return PREFIX + "/leaveInfo_detail.html";
    }

    /**
     * 查询列表
     *
     * @author stylefeng
     * @Date 2019-02-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(LeaveInfoParam leaveInfoParam) {
        Page page = LayuiPageFactory.defaultPage();
        List<LeaveInfoResult> results = leaveInfoService.getLeaveInfoList(page, leaveInfoParam);
        page.setRecords(results);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 请假信息详情
     */
    @ResponseBody
    @RequestMapping(value = "/detail")
    public Object detail(Long leaveInfoId) {
        LeaveInfoPO leaveInfoPO = leaveInfoService.getById(leaveInfoId);
        LeaveInfoResult leaveInfoResult = new LeaveInfoResult();
        BeanUtils.copyProperties(leaveInfoPO, leaveInfoResult);
        Map<String, String> stuMap = leaveInfoService.convertList2MapForStu();
        //学生姓名
        leaveInfoResult.setLeaveUserName(stuMap.get(leaveInfoPO.getLeaveUserNum()));
        //请假类型
        //0--<3
        if (0 == leaveInfoPO.getLeaveInfoType()) {
            leaveInfoResult.setLeaveInfoTypeText("少于三天");
            //3<--1--<7
        } else if (1 == leaveInfoPO.getLeaveInfoType()) {
            leaveInfoResult.setLeaveInfoTypeText("超过三天少于一周");
            //2-->7
        } else {
            leaveInfoResult.setLeaveInfoTypeText("超过一周");
        }
        Map<String, Object> leaveUserMap = BeanUtil.beanToMap(leaveInfoResult);
        return ResponseData.success(leaveUserMap);
    }

    /**
     * 通过
     */
    @RequestMapping("/pass")
    @ResponseBody
    public ResponseData pass(Long leaveInfoId,Integer type) {
        return leaveInfoService.approved(leaveInfoId,type);

    }

    /**
     * 销假
     */
    @RequestMapping("/destroy")
    @ResponseBody
    public ResponseData destroy(Long leaveInfoId) {
        return leaveInfoService.destroy(leaveInfoId);

    }
}


